<template>
  <div>
    <div class="banner" :style="cover">
      <h1 class="banner-title">个人中心</h1>
    </div>
    <v-card class="blog-container">
      <div>
        <span class="info-title">基本信息</span>
      </div>
      <v-row class="info-wrapper">
        <v-col md="3" cols="12">
          <el-upload
            class="avatar-uploader"
            accept="image/jpg,image/jpeg,image/png"
            action="/api/users/avatar"
            :headers="headers"
            :show-file-list="false"
            :on-success="uploadAvatar"
            :before-upload="beforeAvatarUpload"
          >
            <img :src="this.userInfo.avatar" class="avatar"/>
          </el-upload>
        </v-col>
        <v-col md="7" cols="12">
          <v-text-field
            v-model="userInfo.nickname"
            label="昵称"
            placeholder="请输入您的昵称"
          />
          <v-text-field
            v-model="userInfo.webSite"
            class="mt-7"
            label="个人网站"
            placeholder="http://你的网址"
          />
          <v-text-field
            v-model="userInfo.intro"
            class="mt-7"
            label="简介"
            placeholder="介绍下自己吧"
          />
          <!-- <div v-if="loginType != 0" class="mt-7 binding"> -->
          <div class="mt-7 binding">
            <v-text-field
              disabled
              v-model="email"
              label="邮箱号"
              placeholder="请绑定邮箱"
            />
            <v-btn v-if="email" text small @click="openEmailModel">
              修改绑定
            </v-btn>
            <v-btn v-else text small @click="openEmailModel">
              绑定邮箱
            </v-btn>
          </div>
          <v-btn @click="updataUserInfo" outlined class="mt-5">修改</v-btn>
        </v-col>
      </v-row>
    </v-card>
  </div>
</template>

<script>
import * as imageConversion from "image-conversion";
import store from "../../store";
// import AvatarCropper from "vue-avatar-cropper";
export default {
  // components: { AvatarCropper },
  data: function() {
    return {
      headers:{
        token: store.state.token
      },
      userInfo: {
        nickname: this.$store.state.nickname,
        intro: this.$store.state.intro,
        webSite: this.$store.state.webSite,
        // loginType: this.$store.state.loginType,
        avatar: this.$store.state.avatar ? this.$store.state.avatar : this.$store.state.blogInfo.websiteConfig.userAvatar,
      },
      oldAvatar: "",
    };
  },
  methods: {
    beforeAvatarUpload(file) {
        return new Promise(resolve => {
          if (file.size / 1024 < 200) {
            resolve(file);
          }
          // 压缩到200KB,这里的200就是要压缩的大小,可自定义
          imageConversion
            .compressAccurately(file, 200)
            .then(res => {
              resolve(res);
          });
        });
    },
    updataUserInfo() {
      if(this.userInfo.nickname.trim() === ""){
        this.$toast({ type: "error", message: "昵称不能为空" });
        return false;
      }
      if(this.userInfo.avatar.trim() === ""){
        this.userInfo.avatar = this.$store.state.blogInfo.websiteConfig.userAvatar;
      }
      this.axios.put("/api/users/info", this.userInfo).then(({ data }) => {
        if (data.flag) {
          this.$store.commit("updateAvatar", this.userInfo.avatar);
          this.$store.commit("updateUserInfo", this.userInfo);
          this.$toast({ type: "success", message: "修改成功  (❁´◡`❁)*✲ﾟ*" });
        } else {
          this.$toast({ type: "error", message: data.message });
        }
      });
    },
    uploadAvatar(data) {
      if (data.flag) {
        this.userInfo.avatar = data.data;
        this.$toast({ type: "success", message: "上传成功,请及时保存修改  (❁´◡`❁)*✲ﾟ*" });
      } else {
        this.$toast({ type: "error", message: data.message });
      }
    },
    openEmailModel() {
      this.$store.state.emailFlag = true;
    }
  },
  computed: {
    email() {
      return this.$store.state.email;
    },
    loginType() {
      return this.$store.state.loginType;
    },
    cover() {
      var cover = "";
      this.$store.state.blogInfo.pageList.forEach(item => {
        if (item.pageLabel == "user") {
          cover = item.pageCover;
        }
      });
      return "background: url(" + cover + ") center center / cover no-repeat";
    }
  }
};
</script>

<style scoped>
.avatar-uploader {
  margin-bottom: 5rem;
  width: 170px;
  height: 170px;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar {
  width: 170px;
  height: 170px;
  border-radius: 50%;
}

.info-title {
  font-size: 1.25rem;
  font-weight: bold;
}
.info-wrapper {
  margin-top: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
#pick-avatar {
  outline: none;
}
.binding {
  display: flex;
  align-items: center;
}
</style>
